﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Windows.Forms;
using SuperListEx.Utility.Collections;
using System.Xml;
using System.Linq;

namespace SuperListEx
{
    internal class GroupIdentifier : RowIdentifier
    {
        #region vars
        public int Start;
        public int End;
        private int _hashCode;
        private int _columnID;
        #endregion

        #region Properties
        public override List<Column> GroupColumns
        {
            get;
            protected set;
        }

        public override List<object> GroupValues
        {
            get;
            protected set;
        }


        public override List<object> Items
        {
            get;
            protected set;
        }

        public override int ItemCount
        {
            get
            {
                return End - Start;
            }
        }
        #endregion

        #region constructor
        public GroupIdentifier(int start, List<object> listItems, ColumnList columns, int groupIndex, object item)
        {
            Items = new List<object>();
            EventingList<Column> groupColumns = columns.GroupedItems;
            GroupValues = new List<object>(groupIndex + 1);
            GroupColumns = new List<Column>(groupIndex + 1);            
            _hashCode = 0;
            string _matchValue = "";

            for (int i = 0; i <= groupIndex; i++)
            {
                if (groupColumns[i].GroupItemAccessorWithParameter != null)
                {
                    _matchValue = groupColumns[i].GroupItemAccessorWithParameter(item, groupColumns[i].AccessorParameter).ToString();
                }

                else if (groupColumns[i].GroupItemAccessor != null)
                {
                    _matchValue = groupColumns[i].GroupItemAccessor(item).ToString();
                }

                else if (groupColumns[i].ColumnItemAccessorWithParameter != null)
                {
                    _matchValue = groupColumns[i].ColumnItemAccessorWithParameter(item, groupColumns[i].AccessorParameter).ToString();
                }

                GroupValues.Add(_matchValue);
                GroupColumns.Add(groupColumns[i]);

                if (!Items.Contains(item)) Items.Add(item);

                _hashCode += _matchValue.GetHashCode();
                _columnID = i;
            }
            
            Start = start;
            End = start;
        }

        /// <summary>
        /// Used only for serialization.
        /// </summary>
        /// <param name="groupValues"></param>
        /// <param name="groupColumn"></param>
        public GroupIdentifier(List<object> groupValues, Column groupColumn)
        {
            _hashCode = 0;
            foreach (object o in groupValues)
            {
                _hashCode += o.GetHashCode();
            }

            Start = -1;
            End = -1;
            Items = null;
            GroupValues = groupValues;
            GroupColumns = new List<Column>() { groupColumn };
        }
        #endregion

        #region Object overrides
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(this, obj))
            {
                return true;
            }

            GroupIdentifier other = obj as GroupIdentifier;
            if (ReferenceEquals(other, null) || other.GroupValues.Count != GroupValues.Count)
            {
                return false;
            }

            for (int i = 0; i < GroupValues.Count; i++)
            {
                if (!GroupValues[i].Equals(other.GroupValues[i]))
                {
                    return false;
                }
            }

            return true;
        }

        public override int GetHashCode()
        {
            return _hashCode;
        }
        #endregion
    }
}
